package com.mrd.shiptask_shipowner.unit_tools;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class ButtonTool {

	/**
	 * 统一按钮禁用5秒钟
	 * 
	 * @param ctx
	 * @param btns
	 */
	public static void onClickButtonStyle(final Activity ctx, Button[] btns) {
		for (Button btn : btns) {
			try {
				btn.setOnTouchListener(new OnTouchListener() {
					@SuppressLint("NewApi")
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						Button b = (Button) v;
						if (event.getAction() == MotionEvent.ACTION_UP) {
							b.callOnClick();
							if (b.isEnabled()) {
								setBtnEnable(b, ctx);
							}
						}
						return false;
					}
				});
			} catch (Exception e) {
			}
		}
	}

	public static void setBtnEnable(final Button btn, Context ctx) {
		if (!FunctionOfActivity.isNetworkAvailable(ctx)) {
			FunctionOfActivity.makeToaskShow(ctx, "暂无网络,请检测网络后继续操作");
		}

		btn.setEnabled(false);
		btn.setTextColor(Color.GRAY);
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				btn.setEnabled(true);
				btn.setTextColor(Color.BLACK);
			}
		}, 3000);
	}

}
